/*
 ============================================================================
 Name		: CelestialSphereAppUi.cpp
 Author	  : Den Grigorenko
 Copyright   : Copyright (c) 2008 Den123
 Description : CCelestialSphereAppUi implementation
 ============================================================================
 */

// INCLUDE FILES
#include <avkon.hrh>
#include <aknmessagequerydialog.h>
#include <aknnotewrappers.h>
#include <stringloader.h>

#include "SimpleTimer.h"

#include <CelestialSphere_0xEEC98BE4.rsg>

#include "CelestialSphere.hrh"
#include "CelestialSphere.pan"
#include "CelestialSphereApplication.h"
#include "CelestialSphereAppUi.h"
#include "CelestialSphereAppView.h"

// ============================ MEMBER FUNCTIONS ===============================


// -----------------------------------------------------------------------------
// CCelestialSphereAppUi::ConstructL()
// Symbian 2nd phase constructor can leave.
// -----------------------------------------------------------------------------
//
void CCelestialSphereAppUi::ConstructL()
{
	// Initialise app UI with standard value.
	BaseConstructL(CAknAppUi::EAknEnableSkin);

	iTimer = CSimpleTimer :: NewL( CActive::EPriorityStandard, *this );
		
	iReader = CPositionReader :: NewL( this );
	iReader->ReadSatelliteInfo();
			
		
	// Create view object
	iAppView = CCelestialSphereAppView::NewL(ClientRect() );
	
	
    // Hide the status pane
    CEikStatusPane* pStatusPane = iEikonEnv->AppUiFactory()->StatusPane();
    pStatusPane->MakeVisible(EFalse);    
	
}

// -----------------------------------------------------------------------------
// CCelestialSphereAppUi::CCelestialSphereAppUi()
// C++ default constructor can NOT contain any code, that might leave.
// -----------------------------------------------------------------------------
//
CCelestialSphereAppUi::CCelestialSphereAppUi()
{
	// No implementation required
}

// -----------------------------------------------------------------------------
// CCelestialSphereAppUi::~CCelestialSphereAppUi()
// Destructor.
// -----------------------------------------------------------------------------
//
CCelestialSphereAppUi::~CCelestialSphereAppUi()
{
	if( iAppView )
	{
		delete iAppView;
		iAppView = NULL;
	}
	
	if( iReader )
	{
		delete iReader;
		iReader = NULL;
	}
	
	if( iTimer )
	{
		delete iTimer;
		iTimer = NULL;
	}	
}


TInt KWaitTime = 3000000; // 3 sec
void CCelestialSphereAppUi::ReadingComplete( TPositionInfoBase& aPosInfo )
{
   // reading completed -> update view
   TPositionSatelliteInfo& info = (TPositionSatelliteInfo&)aPosInfo;
   iAppView->Update( info );
   iTimer->After( KWaitTime );		
}

void CCelestialSphereAppUi::ReadingError( TInt aErrorNo )
{
   // reading completed with error -> update view
   iAppView->UpdateError( aErrorNo );
   iTimer->After( KWaitTime );		
}

void CCelestialSphereAppUi::TimerExpired( TAny* /*aTimer*/,TInt /*aError*/ )
{
   if( !iInBackground )
     // read data each 3 seconds
     iReader->ReadSatelliteInfo();
   else
	 // app in background -> stop polling 
	 iAppView->UpdateError( KErrNone );	   
}


// -----------------------------------------------------------------------------
// CCelestialSphereAppUi::HandleCommandL()
// Takes care of command handling.
// -----------------------------------------------------------------------------
//
void CCelestialSphereAppUi::HandleCommandL(TInt aCommand)
{
	switch (aCommand)
	{
	case EEikCmdExit:
	case EAknSoftkeyExit:
		Exit();
		break;
	
	case EAbout:
	{
		CAknMessageQueryDialog* dlg = new (ELeave)CAknMessageQueryDialog();
		dlg->PrepareLC(R_ABOUT_QUERY_DIALOG);
		HBufC* title = iEikonEnv->AllocReadResourceLC(R_ABOUT_DIALOG_TITLE);
		dlg->QueryHeading()->SetTextL(*title);
		CleanupStack::PopAndDestroy(); //title
		HBufC* msg = iEikonEnv->AllocReadResourceLC(R_ABOUT_DIALOG_TEXT);
		dlg->SetMessageTextL(*msg);
		CleanupStack::PopAndDestroy(); //msg
		dlg->RunLD();
	}   break;
	
	default:
		Panic( ECelestialSphereUi );
		break;
	}
}
// -----------------------------------------------------------------------------
//  Called by the framework when the application status pane
//  size is changed.  Passes the new client rectangle to the
//  AppView
// -----------------------------------------------------------------------------
//
void CCelestialSphereAppUi::HandleStatusPaneSizeChange()
{
	iAppView->SetRect(ClientRect() );
}

void CCelestialSphereAppUi::HandleResourceChangeL(TInt aType)
{
	CAknAppUi::HandleResourceChangeL(aType);

 	 // ADDED FOR SCALABLE UI SUPPORT
	 //if ( aType == KEikDynamicLayoutVariantSwitch )
	 //	iAppView->SetExtentToWholeScreen();
}

void CCelestialSphereAppUi :: HandleForegroundEventL( TBool  aForeground )
{
	CAknAppUi :: HandleForegroundEventL( aForeground );
	iInBackground = !aForeground;
	if( aForeground )
       iReader->ReadSatelliteInfo();
}




// End of File
